﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;

namespace _4_9
{
    using System;
    using System.Windows.Forms;

    namespace sy4_9
    {
        public partial class MainForm : Form
        {
            private MenuStrip menuStrip1;
            private ToolStripMenuItem menuFile;
            private ToolStripMenuItem menuNew;
            private ToolStripMenuItem menuOpen;
            private ToolStripMenuItem menuSave;
            private ToolStripMenuItem menuExit;
            private ToolStripMenuItem menuFormat;
            private ListBox listBox1;

            public MainForm()
            {
                InitializeComponent();
            }

            private void InitializeComponent()
            {
                this.menuStrip1 = new MenuStrip();
                this.menuFile = new ToolStripMenuItem();
                this.menuNew = new ToolStripMenuItem();
                this.menuOpen = new ToolStripMenuItem();
                this.menuSave = new ToolStripMenuItem();
                this.menuExit = new ToolStripMenuItem();
                this.menuFormat = new ToolStripMenuItem();
                this.listBox1 = new ListBox();
                this.SuspendLayout();

                this.menuNew.Text = "新建(N)...";
                this.menuNew.Click += MenuNew_Click;
                this.menuOpen.Text = "打开(O)...";
                this.menuSave.Text = "保存(S)...";
                this.menuExit.Text = "退出";
                this.menuExit.Click += MenuExit_Click;
                this.menuFile.Text = "文件(F)";
                this.menuFile.DropDownItems.AddRange(new ToolStripItem[] { this.menuNew, this.menuOpen, this.menuSave, this.menuExit });

                this.menuFormat.Text = "格式(O)";
                this.menuStrip1.Items.AddRange(new ToolStripItem[] { this.menuFile, this.menuFormat });
                this.Controls.Add(this.menuStrip1);

                this.listBox1.Dock = DockStyle.Fill;
                this.Controls.Add(this.listBox1);

                this.Text = "计算机配置2";
                this.Size = new Size(400, 300);
                this.ResumeLayout(false);
                this.PerformLayout();
            }

            private void MenuNew_Click(object sender, EventArgs e)
            {
                ConfigForm configForm = new ConfigForm(this);
                configForm.ShowDialog();
            }

            private void MenuExit_Click(object sender, EventArgs e)
            {
                Application.Exit();
            }

            public void UpdateListBox(string[] configs)
            {
                listBox1.Items.Clear();
                foreach (string config in configs)
                {
                    listBox1.Items.Add(config);
                }
            }
        }

        public class ConfigForm : Form
        {
            private ListBox listBoxBrand;
            private RadioButton radioLaptop;
            private RadioButton radioDesktop;
            private RadioButton radioPad;
            private TextBox textBoxMemory;
            private RadioButton radioCPUi;
            private RadioButton radioCPUP;
            private RadioButton radioCPUAMD;
            private CheckBox checkHDD;
            private CheckBox checkDVD;
            private CheckBox checkKeyboard;
            private ListBox listBoxConfig;
            private Button buttonOK;
            private Button buttonReturn;
            private MainForm mainForm;

            public ConfigForm(MainForm main)
            {
                mainForm = main;
                InitializeComponent();
                listBoxBrand.Items.AddRange(new string[] { "ThinkPad", "hp", "Lenovo", "Dell", "acer", "ASUS", "SAMSUNG" });
                listBoxBrand.SelectedIndex = 0;
                textBoxMemory.Text = "4";
            }

            private void InitializeComponent()
            {
                this.listBoxBrand = new ListBox();
                this.radioLaptop = new RadioButton();
                this.radioDesktop = new RadioButton();
                this.radioPad = new RadioButton();
                this.textBoxMemory = new TextBox();
                this.radioCPUi = new RadioButton();
                this.radioCPUP = new RadioButton();
                this.radioCPUAMD = new RadioButton();
                this.checkHDD = new CheckBox();
                this.checkDVD = new CheckBox();
                this.checkKeyboard = new CheckBox();
                this.listBoxConfig = new ListBox();
                this.buttonOK = new Button();
                this.buttonReturn = new Button();
                this.SuspendLayout();

                this.listBoxBrand.Location = new Point(20, 20);
                this.listBoxBrand.Size = new Size(100, 120);
                this.listBoxBrand.SelectedIndexChanged += ListBoxBrand_SelectedIndexChanged;

                this.radioLaptop.Text = "笔记本";
                this.radioLaptop.Location = new Point(150, 20);
                this.radioLaptop.Checked = true;
                this.radioDesktop.Text = "台式机";
                this.radioDesktop.Location = new Point(150, 50);
                this.radioPad.Text = "平板电脑";
                this.radioPad.Location = new Point(150, 80);
                this.radioLaptop.CheckedChanged += DeviceType_CheckedChanged;
                this.radioDesktop.CheckedChanged += DeviceType_CheckedChanged;
                this.radioPad.CheckedChanged += DeviceType_CheckedChanged;

                this.textBoxMemory.Location = new Point(280, 20);
                this.textBoxMemory.Size = new Size(50, 20);
                this.textBoxMemory.TextChanged += TextBoxMemory_TextChanged;

                this.radioCPUi.Text = "酷睿";
                this.radioCPUi.Location = new Point(20, 160);
                this.radioCPUi.Checked = true;
                this.radioCPUP.Text = "奔腾";
                this.radioCPUP.Location = new Point(20, 190);
                this.radioCPUAMD.Text = "AMD";
                this.radioCPUAMD.Location = new Point(20, 220);
                this.radioCPUi.CheckedChanged += CPU_CheckedChanged;

                this.checkHDD.Text = "移动硬盘";
                this.checkHDD.Location = new Point(150, 160);
                this.checkDVD.Text = "外置DVD光驱";
                this.checkDVD.Location = new Point(150, 190);
                this.checkDVD.Checked = true;
                this.checkKeyboard.Text = "无线键鼠套装";
                this.checkKeyboard.Location = new Point(150, 220);
                this.checkKeyboard.Checked = true;
                this.checkHDD.CheckedChanged += Peripheral_CheckedChanged;
                this.checkDVD.CheckedChanged += Peripheral_CheckedChanged;
                this.checkKeyboard.CheckedChanged += Peripheral_CheckedChanged;

                this.listBoxConfig.Location = new Point(280, 60);
                this.listBoxConfig.Size = new Size(150, 200);

                this.buttonOK.Text = "确定";
                this.buttonOK.Location = new Point(280, 270);
                this.buttonOK.Click += ButtonOK_Click;
                this.buttonReturn.Text = "返回";
                this.buttonReturn.Location = new Point(350, 270);
                this.buttonReturn.Click += ButtonReturn_Click;

                this.Controls.Add(this.listBoxBrand);
                this.Controls.Add(this.radioLaptop);
                this.Controls.Add(this.radioDesktop);
                this.Controls.Add(this.radioPad);
                this.Controls.Add(this.textBoxMemory);
                this.Controls.Add(this.radioCPUi);
                this.Controls.Add(this.radioCPUP);
                this.Controls.Add(this.radioCPUAMD);
                this.Controls.Add(this.checkHDD);
                this.Controls.Add(this.checkDVD);
                this.Controls.Add(this.checkKeyboard);
                this.Controls.Add(this.listBoxConfig);
                this.Controls.Add(this.buttonOK);
                this.Controls.Add(this.buttonReturn);

                this.Text = "新建配置清单";
                this.Size = new Size(450, 320);
                this.ResumeLayout(false);
                this.PerformLayout();
                UpdateConfigList();
            }

            private void ListBoxBrand_SelectedIndexChanged(object sender, EventArgs e)
            {
                UpdateConfigList();
            }

            private void DeviceType_CheckedChanged(object sender, EventArgs e)
            {
                UpdateConfigList();
            }

            private void TextBoxMemory_TextChanged(object sender, EventArgs e)
            {
                UpdateConfigList();
            }

            private void CPU_CheckedChanged(object sender, EventArgs e)
            {
                UpdateConfigList();
            }

            private void Peripheral_CheckedChanged(object sender, EventArgs e)
            {
                UpdateConfigList();
            }

            private void UpdateConfigList()
            {
                listBoxConfig.Items.Clear();
                listBoxConfig.Items.Add(listBoxBrand.SelectedItem.ToString());
                if (radioLaptop.Checked) listBoxConfig.Items.Add("笔记本");
                if (radioDesktop.Checked) listBoxConfig.Items.Add("台式机");
                if (radioPad.Checked) listBoxConfig.Items.Add("平板电脑");
                listBoxConfig.Items.Add(textBoxMemory.Text + "G内存");
                if (radioCPUi.Checked) listBoxConfig.Items.Add("酷睿CPU");
                if (radioCPUP.Checked) listBoxConfig.Items.Add("奔腾CPU");
                if (radioCPUAMD.Checked) listBoxConfig.Items.Add("AMD CPU");
                if (checkHDD.Checked) listBoxConfig.Items.Add("移动硬盘");
                if (checkDVD.Checked) listBoxConfig.Items.Add("外置DVD光驱");
                if (checkKeyboard.Checked) listBoxConfig.Items.Add("无线键鼠套装");
            }

            private void ButtonOK_Click(object sender, EventArgs e)
            {
                string[] configs = new string[listBoxConfig.Items.Count];
                listBoxConfig.Items.CopyTo(configs, 0);
                mainForm.UpdateListBox(configs);
                this.Close();
            }

            private void ButtonReturn_Click(object sender, EventArgs e)
            {
                this.Close();
            }
        }
    }
}